﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace UdpReceive
{
    public class UdpReceiveData : IDisposable
    {
         #region Fields

        private UdpPeer _udpPeer;
        private int _port = 8890;
        private Dictionary<string, string> _receiveFileManagerList;
        private object _syncLock = new object();

        #endregion

        #region Constructors

        public UdpReceiveData(int port)
        {
            _port = port;
        }
        public UdpReceiveData()
        {
        }
        #endregion

        #region Events

        //public event RequestSendFileEventHandler RequestSendFile;
        public event PrintReceiveDataEventHandler PrintReceiveData;

        public event FileReceiveBufferEventHandler FileReceiveBuffer;

        //public event FileReceiveEventHandler FileReceiveComplete;

        //public event FileReceiveEventHandler FileReceiveCancel;

        #endregion

        #region Properties

        public UdpPeer UdpPeer
        {
            get
            {
                if (_udpPeer == null)
                {
                    _udpPeer = new UdpPeer(_port);
                    _udpPeer.ReceiveData += new ReceiveDataEventHandler(UdpPeerReceiveData);
                }
                return _udpPeer;
            }
        }

        public Dictionary<string, string> ReceiveFileManagerList
        {
            get
            {
                if (_receiveFileManagerList == null)
                {
                    _receiveFileManagerList = new Dictionary<string, string>(10);
                }
                return _receiveFileManagerList;
            }
        }

        public int Port
        {
            get { return _port; }
        }

        #endregion

        #region Methods

        public void Start()
        {
            UdpPeer.Start();
        }     

        private void Send(int messageID, object data, IPEndPoint remoteIP)
        {
            //SendCell cell = new SendCell(messageID, data);
            //UdpPeer.Send(cell, remoteIP);
        }
        /// <summary>
        /// UDP(DdpPeer)接收数据后的调用方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UdpPeerReceiveData(object sender, ReceiveDataEventArgs e)
        {
            SendCell cell = new SendCell();
            cell.FromBuffer(e.Buffer);
            if (cell.CheckCrc)
            {
                switch (cell.OrderID)
                {
                    case (int)OrderType.RequestTreeList:
                        break;
                    case (int)OrderType.RequestUploadFile:
                        break;
                    case (int)OrderType.RequestGetClientIp:
                        break;
                }
            }
            OnRecieveBuffer(cell);
        }

        private void OnRecieveBuffer(SendCell requestData)
        {
            OnPrintReceiveBuffer(new PrintReceiveDataEventArgs(requestData));
        }
        protected virtual void OnPrintReceiveBuffer(PrintReceiveDataEventArgs e)
        {
            if (PrintReceiveData != null)
            {
                PrintReceiveData(this, e);
            }
        }

        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            if(_udpPeer != null)
            {
                _udpPeer.Dispose();
                _udpPeer = null;
            }
            if (_receiveFileManagerList != null &&
                _receiveFileManagerList.Count > 0)
            {
                //foreach (ReceiveFileManager  receiveFileManager in _receiveFileManagerList.Values)
                //{
                //    receiveFileManager.Dispose();
                //}
                _receiveFileManagerList.Clear();
            }
        }

        #endregion
    }
}
